﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ManageMenuBottom()
        {
            return View();
        }
        public ActionResult getJsonMenuByPosition(int ID)
        {
            var langmenu = from m in db.MenuLangs where m.LangID == Request.Cookies["_culture"]["lang"] && m.Menu.MenuPosID == ID select new { m.Name, m.MenuID, m.Link, MenuPositionName=m.Menu.MenuPosition.Name };
            return Json(langmenu,JsonRequestBehavior.AllowGet);
        }
        public ActionResult DeleteMenuByID(int ID)
        {
            var langmenu = from m in db.MenuLangs where  m.MenuID == ID select m;
            db.MenuLangs.DeleteAllOnSubmit(langmenu);
            db.SubmitChanges();
            var menu = db.Menus.Where(m => m.ID == ID).FirstOrDefault();
            db.Menus.DeleteOnSubmit(menu);
            db.SubmitChanges();
            return Json(new { success=true});
        }
        public ActionResult EditMenu(int ID)
        {
            var langproducts = from n in db.ProductLangs where n.LangID == Request.Cookies["_culture"]["lang"] select new {Name= n.DisplayName,Link=Url.Action(n.Product.Action,n.Product.Controller)};
            ViewBag.SlProduct = new SelectList(langproducts, "Link", "Name");
            var langmenu = (from m in db.MenuLangs where m.LangID == Request.Cookies["_culture"]["lang"] && m.MenuID == ID select m).FirstOrDefault();
            return View(langmenu);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult EditMenu(Models.MenuLang model)
        {
            if (ModelState.IsValid)
            {
                var langmenu = (from m in db.MenuLangs where m.LangID == Request.Cookies["_culture"]["lang"] && m.MenuID == model.MenuID select m).FirstOrDefault();
                if (langmenu != null)
                {
                    langmenu.Name = model.Name;
                    langmenu.Link = model.Link;
                    db.SubmitChanges();
                    return RedirectToAction("ManageMenuBottom");
                }
            }
            return View();
        }
        //AddMenuPos
        public ActionResult AddMenuPos(int ID)
        {
            var langproducts = from n in db.ProductLangs where n.LangID == Request.Cookies["_culture"]["lang"] select new { Name =  n.DisplayName, Link = Url.Action(n.Product.Action, n.Product.Controller) };
            ViewBag.SlProduct = new SelectList(langproducts, "Link", "Name");
            ViewBag.Menupostion = db.MenuPositions.Where(m => m.ID == ID).FirstOrDefault();
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult AddMenuPos(int MenuPositionID,Models.MenuLang model)
        {
            if (ModelState.IsValid)
            {
                Models.Menu mn = new Models.Menu();
                mn.Name = model.Name;
                mn.MenuPosID = MenuPositionID;
                db.Menus.InsertOnSubmit(mn);
                db.SubmitChanges();

                Models.MenuLang langmenu = new Models.MenuLang();
                langmenu.Name = model.Name;
                langmenu.MenuID = mn.ID;
                langmenu.Link = model.Link;
                langmenu.LangID = "vi-VN";
                db.MenuLangs.InsertOnSubmit(langmenu);

                Models.MenuLang langmenu2 = new Models.MenuLang();
                langmenu2.Name = model.Name;
                langmenu2.Link = model.Link;
                langmenu2.LangID = "en-US";
                langmenu2.MenuID = mn.ID;
                db.MenuLangs.InsertOnSubmit(langmenu2);
                    db.SubmitChanges();
                    return RedirectToAction("ManageMenuBottom");
               
            }
            return View();
        }
    }
}
